package cn.bjc.system.controller;

import cn.bjc.basic.util.JsonResult;
import cn.bjc.basic.util.PageList;
import cn.bjc.system.domain.Systemdictionarytype;
import cn.bjc.system.query.SystemdictionarytypeQuery;
import cn.bjc.system.service.ISystemdictionarytypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//数据字典-类型-接口
@RestController
@RequestMapping("/type")
public class SystemdictionarytypeController {
    
    @Autowired
    private ISystemdictionarytypeService typeService;
    
    //查询所有
    @GetMapping
    public List<Systemdictionarytype> findAll(){
        return typeService.loadAll();
    }
    
    
    //通过id查询
    @GetMapping("/{id}")
    public Systemdictionarytype findOne(@PathVariable("id") Long id){
        return typeService.loadById(id);
    }
    
    
    //删除
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            typeService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败,稍后重试");
        }
    }

    
    //添加和修改
    @PutMapping
    public JsonResult updateOrAdd(@RequestBody Systemdictionarytype Systemdictionarytype){
        try {
            if(Systemdictionarytype.getId()==null){
                //添加操作
                typeService.save(Systemdictionarytype);
            } else {
                typeService.update(Systemdictionarytype);
            }
            return JsonResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }
    
    
    //分页查询
    @PostMapping()
    public PageList<Systemdictionarytype> queryPage(@RequestBody SystemdictionarytypeQuery query){
        try {
            return typeService.pageQuery(query);
        } catch (Exception e){
            e.printStackTrace();
            return new PageList<Systemdictionarytype>();
        }
    }


    //批量删除操作
    @PatchMapping()
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            typeService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟,稍后重试");
        }
    }
    
    
}
